﻿<?php
$DEBUG_ = false;
class Map{
	private $rows;
	private $columns;
	private $map;
	private $start;
	private $finish;
	public $infinity;	
	public $inputFile;

	const FLAT_WALL = 0;
	private $mode;

	function __construct($file, $mode = Map::FLAT_WALL){
		$handle = fopen("Map/$file", "r");			
		$this->inputFile = "Map/$file";
		$t = fscanf($handle, "%d %d");
		$this->rows = $t[0];
		$this->columns = $t[1];
		$this->map = array();		
		for ($i = 0; $i < $this->rows; $i++) {
			$x = fgets($handle);
			$this->map[$i] = explode(" ", $x, $this->columns);
			//var_dump($this->map[$i]);echo "<br/>";			
		}
		$this->infinity = $this->rows * $this->columns * sqrt(2) + 1;
		//echo "INFINITY = $this->infinity<br/>";
		fclose($handle);
		$this->mode = $mode;
	}

	public function getRows(){
		return $this->rows;
	}

	public function getColumns(){
		return $this->columns;
	}

	public function  printMap(){
		echo '<table id="mapTable" class="map">';
		echo '<tr class="indexC"><td></td>';
		for ($i = 0; $i < $this->columns; $i++) {
			echo "<td>".$i."</td>";
		}
		echo "<td></td>";
		echo '</tr>';

		for ($i = 0; $i < $this->rows; $i++) {
			echo "<tr><td class='indexR'>".$i."</td>";
			for ($j = 0; $j < $this->columns; $j++) {
				if($this->mode == Map::FLAT_WALL){
					switch($this->map[$i][$j]){
						case 0:
							echo "
							<td onclick='onCellClick($i, $j);'>
								<div class='layer'>
									<div class='rhs'></div>
									<div class='g'></div>
									<div class='h'></div>
									<div class='f'></div>
								</div>
								<img/>
							";
							break;
						default:
							echo "
							<td class='wall'>
								<div class='layer'>
									<div class='rhs'></div>
									<div class='g'></div>
									<div class='h'></div>
									<div class='f'></div>
								</div>";
					}
					//echo $this->map[$i][$j];
					echo "</td>";
				} else {
					echo "<td onclick='onCellClick($i, $j);'>";
					echo $this->map[$i][$j];
					echo "</td>";
				}
			}
			echo "<td class='indexR'>$i</td>";
			echo "</tr>";
		}
		
		echo '<tr class="indexC"><td></td>';
		for ($i = 0; $i < $this->columns; $i++) {
			echo "<td>".$i."</td>";
		}
		echo '</tr>';		
		echo '</table>';
	}

	public function generateJSCode(){
		echo '<script type="text/javascript">';

		//TERRAIN
		echo "var terrain = [";
		for ($i = 0; $i < $this->rows; $i++) {
			echo "[";
			for ($j = 0; $j < $this->columns; $j++) {
				echo $this->map[$i][$j].", ";
			}
			echo "],
			";
		}
		echo "];
		";

		//COORDINATE STRUCTURE
		echo '
		const BETTER = 1;
		const WORSE = -1;
		
		const NEW = 0;
		const IN_OPEN_SET = 1;
		const IN_CLOSED_SET = 2;
		const RAISED = 3;
		const LOW = 4;
		function Coordinate(x, y, cost){
			this.x = x;
			this.y = y;
			this.cost = cost;			
			this.heuristic = INFINITY;
			this.status = NEW;
			this.isInPath = false;
		}
		
		';

		//MAP
		echo "var map = [";
		for ($i = 0; $i < $this->rows; $i++) {
			echo "[";
			for ($j = 0; $j < $this->columns; $j++) {
				echo "new Coordinate($i, $j, ".$this->infinity."), ";
			}
			echo "],
			";
		}
		echo "];
		";

		//SIZE & MAXVAL
		echo "
		var m = map.length;
		var n = map[0].length;
		var INFINITY = $this->infinity;		
		";
								
		//SAVE MAP FUNCTION
		echo '
		function save(){		
			var a =newAjaxInstance();
			a.onreadystatechange = function(){
				if(isFinished(a) == 1){
					var result = a.responseText;
					if(result == "true"){						
						alert("Đã lưu thành công!");
					}else {
						alert("Không thể lưu được bản đồ!");
					}
				}
			}
			var params = "m="+m+"&n="+n+"&f='.$this->inputFile.'&table=";
			for(var i=0; i<m; ++i){
				for(var j=0; j<n; ++j){
					params += terrain[i][j] + "-";
				}
				params += "|";
			}
			requestPost(a, params, "save.php");
			
			//Update form
			document.searchFrm.searchBtn.disabled = "";
			document.searchFrm.changeBtn.checked = false;
		}
		';
		
		echo '</script>';
		echo '<script language="javascript" src="JS/main.js"></script>';

		//ROUTE
		echo '
		<div id="startImg">
			<img src="Images/mario.png" onclick="if(isSetting==SETTING_NOTHING){beginSettingStart(-1, -1);document.getElementById(\'startImg\').style.display=\'none\';}">Bắt đầu
		</div>
		<div id="finishImg"> 
			<img src="Images/mushroom.png" onclick="if(isSetting==SETTING_NOTHING){beginSettingFinish(-1, -1);document.getElementById(\'finishImg\').style.display=\'none\';}">Kết thúc<br/>
		</div>
		<form name="route">
			Tìm kiếm đường đi từ (<input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="fromX" value="-1"/>,<input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="fromY" value="-1"/>)
			tới (<input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="toX" value="-1"/>, <input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="toY" value="-1"/>)
		</form>
		';
		
		//DEFAULT VALUES
		echo '
		<script type="text/javascript">
		document.route.fromX.value = -1;
		document.route.fromY.value = -1;
		document.route.toX.value = -1;
		document.route.toY.value = -1;
		</script>
		';				
	}

	public function generateJSCode2(){
		echo '<script type="text/javascript">';

		echo "var INFINITY = $this->infinity;";
		
		//TERRAIN
		echo "var terrain = [";
		for ($i = 0; $i < $this->rows; $i++) {
			echo "[";
			for ($j = 0; $j < $this->columns; $j++) {
				echo $this->map[$i][$j].", ";
			}
			echo "],
			";
		}
		echo "];
		";

		//COORDINATE STRUCTURE
		echo '
		const BETTER = 1;
		const WORSE = -1;	
		
		const NEW = 0;
		const IN_OPEN_SET = 1;
		const IN_CLOSED_SET = 2;
		const RAISED = 3;
		const LOW = 4;
		function QueueKey(k1, k2){
			this.k1 = k1;
			this.k2 = k2;
		}
		function Coordinate(x, y, cost){
			this.x = x;
			this.y = y;
			this.cost = cost;			
			this.status = NEW;
			this.heuristic = INFINITY;
			this.rhs = INFINITY;
			this.key = new QueueKey(INFINITY, INFINITY);
			this.turn = 0;
			this.isInPath = false;
		}		
		function NewCost(x, y, cost){
			this.x = x;
			this.y = y;
			this.cost = cost;
		}		
		';

		//MAP
		echo "var map = [";
		for ($i = 0; $i < $this->rows; $i++) {
			echo "[";
			for ($j = 0; $j < $this->columns; $j++) {
				echo "new Coordinate($i, $j, ".$this->infinity."), ";
			}
			echo "],
			";
		}
		echo "];
		";

		//SIZE & MAXVAL
		echo "
		var m = map.length;
		var n = map[0].length;		
		";
								
		//SAVE MAP FUNCTION
		echo '
		function save(){		
			var a =newAjaxInstance();
			a.onreadystatechange = function(){
				if(isFinished(a) == 1){
					var result = a.responseText;
					if(result == "true"){						
						alert("Đã lưu thành công!");
					}else {
						alert("Không thể lưu được bản đồ!");
					}
				}
			}
			var params = "m="+m+"&n="+n+"&f='.$this->inputFile.'&table=";
			for(var i=0; i<m; ++i){
				for(var j=0; j<n; ++j){
					params += terrain[i][j] + "-";
				}
				params += "|";
			}
			requestPost(a, params, "save.php");
			
			//Update form
			document.searchFrm.searchBtn.disabled = "";
			document.searchFrm.changeBtn.checked = false;
		}
		';
		
		echo '</script>';
		echo '<script language="javascript" src="JS/main.js"></script>';

		//ROUTE
		echo '
		<div id="startImg">
			<img src="Images/mario.png" onclick="if(isSetting==SETTING_NOTHING){beginSettingStart(-1, -1);document.getElementById(\'startImg\').style.display=\'none\';}">Bắt đầu
		</div>
		<div id="finishImg"> 
			<img src="Images/mushroom.png" onclick="if(isSetting==SETTING_NOTHING){beginSettingFinish(-1, -1);document.getElementById(\'finishImg\').style.display=\'none\';}">Kết thúc<br/>
		</div>
		<form name="route">
			Tìm kiếm đường đi từ (<input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="fromX" value="-1"/>,<input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="fromY" value="-1"/>)
			tới (<input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="toX" value="-1"/>, <input type="text" style="width: 30px;background-color: #FFF;border: none;" readonly="readonly" name="toY" value="-1"/>)
		</form>
		';
		
		//DEFAULT VALUES
		echo '
		<script type="text/javascript">
		document.route.fromX.value = -1;
		document.route.fromY.value = -1;
		document.route.toX.value = -1;
		document.route.toY.value = -1;
		</script>
		';				
	}	
}

function generateJSDebugCode($debugIsEnabled){
	echo '
	<script type="text/javascript">
	function alertDebug(msg){
		'.(($debugIsEnabled===true)?'alert(msg);':'').'
	}
	function logDebug(msg){
		'.(($debugIsEnabled===true)?'log(msg);':'').'
	}
	</script>
	';
}
?>